package com.net.entity;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class TankMessageDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if(byteBuf.readableBytes() < 8) {
            return;
        }
        byteBuf.markReaderIndex();
        MessageType messageType = MessageType.values()[byteBuf.readInt()];
        int length = byteBuf.readInt();
        if(byteBuf.readableBytes()< length) {
            byteBuf.resetReaderIndex();
            return;
        }
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        Message msg = (Message) Class.forName("com.net.entity." + messageType.toString() + "Message").getDeclaredConstructor().newInstance();

        msg.parse(bytes);
        list.add(msg);
    }
}
